﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Windows.Resources;
using System.Windows.Media.Imaging;

namespace Sanctuary.Core.Images
{
    public class Backgrounds
    {
        public const string FILE_NAME = "BackgroundDefinition.xml";
        private static StreamResourceInfo zipInfo;

        public Backgrounds(XDocument document, StreamResourceInfo zip)
        {
            zipInfo = zip;

            foreach (XElement item in document.Descendants("Background") )
            {
                Background bg = new Background(){ 
                    Name = item.Attribute("Name").Value, 
                    Path = item.Value };
                backgroundItems.Add(bg.Name, bg);
            }
        }

        private Dictionary<string, Background> backgroundItems = new Dictionary<string, Background>();

        public Background this[string i]
        {
            get
            {
                if (!backgroundItems.ContainsKey(i))
                {
                    throw new GameContentException("Background: " + i + " not conatined in " + FILE_NAME);
                }
                return backgroundItems[i];
            }
        }

        public void LoadBackground(Image image, string name)
        {
            StreamResourceInfo sr = Application.GetResourceStream(zipInfo, new Uri(this[name].Path, UriKind.Relative));
            BitmapImage bmp = new BitmapImage();
            bmp.SetSource(sr.Stream);
            image.Source = bmp;
        }
    }
}
